package com.baizhi.cmfz.service.impl;

import com.baizhi.cmfz.annotation.AddCacheAnnotation;
import com.baizhi.cmfz.annotation.DeleteCahceAnnotation;
import com.baizhi.cmfz.dao.BannerDao;
import com.baizhi.cmfz.entity.Banner;
import com.baizhi.cmfz.service.BannerService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class BannerServiceImpl implements BannerService {
	private Logger logger = LoggerFactory.getLogger(BannerServiceImpl.class);

	@Autowired(required = false)
	private BannerDao bannerDao;
	@Override
	@Transactional(readOnly = true)
	@AddCacheAnnotation
	public Map<String ,Object> selectBannerPage(int page, int limit) {
		logger.debug("参数："+page+"  "+limit);
//		显示的数据的起始行数,在mysql中不包含本条数据,从下一行数据开始
		int offset = (page-1)*limit;
		HashMap<String, Object> map = new HashMap<>();
		List<Banner> banners = bannerDao.selectBannerPage(offset, limit);
		int count = bannerDao.selectCount();
		map.put("data",banners);
		map.put("count",count);
		map.put("code",0);
		map.put("msg","");
		return map;
	}


	@Override
	@Transactional
	@DeleteCahceAnnotation
	public Map<String, Object> insertBanner(Banner banner) {
		logger.debug("参数："+banner);
		Map<String,Object> map = new HashMap<>();
		int i = bannerDao.insertBanner(banner);
		if(i > 0){
			map.put("isInsert","上传成功");
		}else{
			map.put("isInsert","上传失败");
		}
		return map;
	}

	@Override
	@Transactional(readOnly = true)
	@AddCacheAnnotation
	public Banner selectById(int id) {
		logger.debug("参数："+id);
		return bannerDao.selectById(id);
	}


	@Override
	@DeleteCahceAnnotation
	public Map updateBanner(Banner banner) {
		logger.debug("参数："+banner);
		Map map = new HashMap();
		int i = bannerDao.updateBanner(banner);
		if(i > 0){
			map.put("isUpdate","成功");
		}else{
			map.put("isUpdate","失败");
		}
		return map;
	}

	@Override
	@Transactional
	@DeleteCahceAnnotation
	public Map deleteBanner(int bannerId) {
		logger.debug("参数："+bannerId);
		Map map = new HashMap();
		int i = bannerDao.deleteBanner(bannerId);
		if(i > 0){
			map.put("isDelete","成功");
		}else{
			map.put("isDelete","失败");
		}
		return map;
	}
}
